package com.tycmc.signelectroniccontracts.tools.adapter.abslistview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import android.text.Html;
import android.text.InputFilter;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;

import com.tycmc.signelectroniccontracts.tools.images.ImageSetting;
import com.tycmc.signelectroniccontracts.tools.manager.AppManager;


/**
 * Created by Administrator on 2016/10/24 0024.
 */

public class AdapterHelper {
    private  SparseArray<View> mViews;
    private static int mPosition;
    private View mConvertView;
    public Context context;

    private AdapterHelper(Context context, ViewGroup parent, int layoutId,
                          int position, int descendantFocusability) {
        mPosition = position;
        this.context = context;
        this.mViews = new SparseArray<View>();
        mConvertView = LayoutInflater.from(context.getApplicationContext()).inflate(layoutId, parent,
                false);

        // setTag
        mConvertView.setTag(this);
    }

    /**
     * 拿到一个ViewHolder对象
     *
     * @param context
     * @param convertView
     * @param parent
     * @param layoutId
     * @param position
     * @return
     */
    public static AdapterHelper get(Context context, View convertView,
                                    ViewGroup parent, int layoutId, int position, int descendantFocusability) {
        mPosition = position;
        if (convertView == null) {
            return new AdapterHelper(context, parent, layoutId, position, descendantFocusability);
        }
        ((ViewGroup) convertView).setDescendantFocusability(descendantFocusability != -1 ? descendantFocusability : ViewGroup.FOCUS_BLOCK_DESCENDANTS);
        return (AdapterHelper) convertView.getTag();
    }

    public View getConvertView() {
        return mConvertView;
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(@IdRes int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = mConvertView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public AdapterHelper setText(int viewId, String text) {
        TextView view = getView(viewId);
//        view.setSelected(true);
//        view.setMarqueeRepeatLimit(2);
//        view.setEllipsize(TextUtils.TruncateAt.MARQUEE);
//        view.setFocusableInTouchMode(true);

        view.setText(text);
        return this;
    }

    /**
     * 显示HTML
     *
     * @param viewId
     * @param text
     * @return
     */
    public AdapterHelper setTextHtml(int viewId, String text) {
        TextView view = getView(viewId);
        view.setText(Html.fromHtml(text));
        return this;
    }

    public AdapterHelper setFilters(int viewId, InputFilter[] inputFilters) {
        EditText view = getView(viewId);
        view.setFilters(inputFilters);
        return this;
    }

    public AdapterHelper addTextChangedListener(int viewId, TextWatcher textWatcher) {
        EditText view = getView(viewId);
        view.addTextChangedListener(textWatcher);
        return this;
    }

    public AdapterHelper setOnFocusChangeListener(int viewId, View.OnFocusChangeListener onFocusChangeListener) {
        EditText view = getView(viewId);
        view.setOnFocusChangeListener(onFocusChangeListener);
        return this;
    }

    public AdapterHelper setTextHint(int viewId, String text) {
        TextView view = getView(viewId);
        view.setHint(text);
        return this;
    }

    public AdapterHelper setInputType(int viewId, int text) {
        EditText view = getView(viewId);
        view.setInputType(text);
        return this;
    }


    public AdapterHelper setfouce(int viewId, boolean flag) {
        EditText edittext = getView(viewId);
        edittext.setFocusable(flag);
        edittext.setFocusableInTouchMode(flag);
        if (flag) {
            edittext.requestFocus();
        } else {
            edittext.clearFocus();
        }
        return this;
    }

    public AdapterHelper setDrawableLeft(int viewId, @DrawableRes int drawid) {
        if (drawid == -1) {
            return this;
        }
        TextView view = getView(viewId);
        Drawable drawable = context.getResources().getDrawable(drawid);
/// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(drawable, null, null, null);
        return this;
    }

    public AdapterHelper setDrawableTop(int viewId, @DrawableRes int drawid) {
        if (drawid == -1) {
            return this;
        }
        TextView view = getView(viewId);
        Drawable drawable = context.getResources().getDrawable(drawid);
/// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, drawable, null, null);
        return this;
    }

    public AdapterHelper setDrawableBottom(int viewId, @DrawableRes int drawid) {
        if (drawid == -1) {
            return this;
        }
        TextView view = getView(viewId);
        Drawable drawable = context.getResources().getDrawable(drawid);
/// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, null, null, drawable);
        return this;
    }

    public AdapterHelper setDrawableRight(int viewId, @DrawableRes int drawid) {
        if (drawid == -1) {
            return this;
        }
        TextView view = getView(viewId);
        Drawable drawable = context.getResources().getDrawable(drawid);
/// 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        view.setCompoundDrawables(null, null, drawable, null);
        return this;
    }

    public AdapterHelper setBackgroundColor(int viewId, int color) {
        View view = getView(viewId);
        view.setBackgroundColor(color);

        return this;
    }

    public AdapterHelper setSize(int viewId, int width, int height) {
        View view = getView(viewId);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = height;
        params.width = width;
        view.setLayoutParams(params);
        return this;
    }

    public AdapterHelper setSize(int viewId, int numCount) {
        View view = getView(viewId);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        params.height = AppManager.getAppManager().currentActivity().getWindowManager().getDefaultDisplay().getWidth() * 5 / numCount / 4;
        view.setLayoutParams(params);
        return this;
    }

    public AdapterHelper setBackgroundResource(int viewId, int resouid) {
        View view = getView(viewId);

        view.setBackgroundResource(resouid);
        return this;
    }


    /**
     * 为TextView设置字符串
     *
     * @param viewId
     * @param text
     * @return
     */
    public AdapterHelper setText(int viewId, CharSequence text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    public AdapterHelper setChecked(int viewId, boolean flag) {
        CheckBox view = getView(viewId);
        view.setChecked(flag);
        return this;
    }

    public AdapterHelper setOnCheckedChangeListener(int viewId, CompoundButton.OnCheckedChangeListener listener) {
        CheckBox view = getView(viewId);
        view.setOnCheckedChangeListener(listener);
        return this;
    }

    public AdapterHelper setTextColor(int viewId, int text) {
        TextView view = getView(viewId);
        view.setTextColor(text);
        return this;
    }

    public AdapterHelper setNumColumns(int viewId, int num) {
        GridView view = getView(viewId);
        view.setNumColumns(num);
        return this;
    }

    /**
     * 为TextView字符
     *
     * @param viewId
     * @param text
     * @return
     */
    public AdapterHelper setText(int viewId, SpannableStringBuilder text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    public AdapterHelper setText(int viewId, Spanned text) {
        TextView view = getView(viewId);
        view.setText(text);
        return this;
    }

    /**
     * 点击事件
     *
     * @param viewId
     * @param onClickListener
     * @return
     */
    public AdapterHelper setOnClickListener(int viewId, View.OnClickListener onClickListener) {
        getView(viewId).setOnClickListener(onClickListener);
        return this;
    }

    /**
     * 是否可点
     *
     * @param viewId
     * @param flag
     * @return
     */
    public AdapterHelper setClickable(int viewId, boolean flag) {
        getView(viewId).setClickable(flag);
        return this;
    }

    public AdapterHelper setEnabled(int viewId, boolean flag) {
        getView(viewId).setEnabled(flag);
        return this;
    }

    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param drawableId
     * @return
     */
    public AdapterHelper setImageResource(int viewId, int drawableId) {
        if (drawableId == -1) {
            return this;
        }
        ImageView view = getView(viewId);
        view.setImageResource(drawableId);

        return this;
    }


    /**
     * 为ImageView设置图片
     *
     * @param viewId
     * @param
     * @return
     */
    public AdapterHelper setImageBitmap(int viewId, Bitmap bm) {
        ImageView view = getView(viewId);
        view.setImageBitmap(bm);
        return this;
    }

    /**
     * 设置网络图片
     *
     * @param viewId   图片的id
     * @param imageurl 网络图片的地址
     * @return
     */
    public AdapterHelper setImageUrl(int viewId, String imageurl) {
        ImageView view = getView(viewId);
        ImageSetting.urlImage(imageurl, view);
        return this;
    }


    /**
     * 设置圆形图片
     *
     * @param viewId   图片的id
     * @param imageurl 网络图片的地址
     * @return
     */
    public AdapterHelper setImageUrlCircle(int viewId, String imageurl) {
        ImageView view = getView(viewId);
        ImageSetting.urlImage(imageurl, view, true);
        return this;
    }

    /**
     * 设置隐藏 显示
     *
     * @param viewId
     * @param flag
     * @return
     */
    public AdapterHelper setGone(int viewId, boolean flag) {
        View view = getView(viewId);
        if (flag) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
        return this;
    }

    /**
     * 隐藏
     *
     * @param viewId
     * @return
     */
    public AdapterHelper setGone(int viewId) {
        getView(viewId).setVisibility(View.GONE);
        return this;
    }

    /**
     * 设置不显示 显示
     *
     * @param viewId
     * @param flag
     * @return
     */
    public AdapterHelper setInvisible(int viewId, boolean flag) {
        View view = getView(viewId);
        if (flag) {
            view.setVisibility(View.INVISIBLE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
        return this;
    }

    public AdapterHelper setTag(int viewId, Object o) {
        getView(viewId).setTag(o);
        return this;
    }

    public AdapterHelper setTagView(int viewId, @IdRes int ids) {
        getView(viewId).setTag(getView(ids));
        return this;
    }

    public AdapterHelper setInvisible(int viewId) {
        getView(viewId).setVisibility(View.INVISIBLE);
        return this;
    }

    public AdapterHelper setVisible(int viewId) {
        getView(viewId).setVisibility(View.VISIBLE);
        return this;
    }

    public AdapterHelper setOnItemClickListener(int viewId, AdapterView.OnItemClickListener onItemClickListener) {
        AbsListView view = getView(viewId);
        view.setOnItemClickListener(onItemClickListener);
        return this;
    }

    public AdapterHelper setAdapter(int viewId, BaseAdapter adapter) {
        AbsListView view = getView(viewId);
        view.setAdapter(adapter);
        return this;
    }

    public int getVisible(int viewId) {
        return getView(viewId).getVisibility();
    }

    public int getPosition() {
        return mPosition;
    }

}
